package com.chain.qs.modules.biz.service;

import com.chain.qs.common.service.CrudService;
import com.chain.qs.common.utils.EhCacheUtils;
import com.chain.qs.common.utils.TimeUtils;
import com.chain.qs.modules.biz.dao.WarningRecordDao;
import com.chain.qs.modules.biz.entity.WarningRecord;
import com.chain.qs.modules.sys.service.OfficeService;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 告警日志Service
 *
 */
@Service
@Transactional
public class WarningRecordService extends CrudService<WarningRecordDao, WarningRecord> {
    @Resource
    private EquipmentGroupItemService equipmentGroupItemService;
    @Resource
    private EquipmentParameterService equipmentParameterService;
    @Resource
    StationGroupService sgroupService;
    @Resource
    OfficeService officeService;
    @Resource
    private BasicInfoService basicInfoService;



    public WarningRecord getWarningRecord(String sgroupId, String itemId, String paramKid, String isSuspend, String operator, String timeBegin, String timeEnd) {
        WarningRecord warningRecord = new WarningRecord();
        if (sgroupId != null && !sgroupId.equals("")) {
            warningRecord.setSgroupId(sgroupId);
            warningRecord.setSgroupName(sgroupService.get(sgroupId).getName());
        }
        if (itemId != null && !itemId.equals("") && !itemId.equals("0")) {
            warningRecord.setItemId(itemId);
            warningRecord.setItemName(equipmentGroupItemService.get(itemId).getName());
        }
        if (paramKid != null && !paramKid.equals("")) {
            warningRecord.setParamKid(paramKid);
            warningRecord.setParamName(equipmentParameterService.get(paramKid).getName());
        }
        if (isSuspend != null && !isSuspend.equals("")) {
            warningRecord.setIsSuspend(isSuspend);
        }
        if (operator != null && !operator.equals("")) {
            warningRecord.setOperator(operator);
        }
        if (timeBegin != null && !timeBegin.equals("")) {
            warningRecord.setTimeBegin(timeBegin);
        }
        if (timeEnd != null && !timeEnd.equals("")) {
            warningRecord.setTimeEnd(timeEnd);
        }
        return warningRecord;
    }

    public StringBuffer fileTitle(WarningRecord warningRecord, String officeId) {
        // 生成文件的名称
        StringBuffer fileName = new StringBuffer("");
        // 站名
        String stationName = officeService.get(officeId).getName();
        // 站组名
        String sGroupName = warningRecord.getSgroupName();
        // 设备名称
        String itemName = warningRecord.getItemName();

        if (stationName != null) {
            fileName.append(stationName);
        }
        if (sGroupName != null) {
            fileName.append(sGroupName);
        }
        if (itemName != null) {
            fileName.append(itemName);
        }
        return fileName;
    }

    public void recovyRuleRecord(WarningRecord warningRecord) {
        dao.recovyRuleRecord(warningRecord);
    }

    Map<String, Map<String, Map<String, Map<String, Map<String, String>>>>> warningRecordMap() {
        Map<String, Map<String, Map<String, Map<String, Map<String, String>>>>> warningRecordMap = new HashMap<>();//存储所有触发的规则
        List<WarningRecord> recordList = dao.findList(new WarningRecord());
        for (WarningRecord warningRecord : recordList) {
            String officeKey = warningRecord.getOfficeCode();
            String gKey = warningRecord.getSgroupId();
            String dKey = warningRecord.getItemId();
            String pKey = warningRecord.getParamId();
            String ruleId = warningRecord.getRuleId();
            String isSuspend = warningRecord.getIsSuspend();
            if (warningRecordMap.get(officeKey) == null) {
                warningRecordMap.put(officeKey, new HashMap<String, Map<String, Map<String, Map<String, String>>>>());
            }
            if (warningRecordMap.get(officeKey).get(isSuspend) == null) {
                warningRecordMap.get(officeKey).put(isSuspend, new HashMap<String, Map<String, Map<String, String>>>());
            }
            if (warningRecordMap.get(officeKey).get(isSuspend).get(gKey) == null) {
                warningRecordMap.get(officeKey).get(isSuspend).put(gKey, new HashMap<String, Map<String, String>>());
            }
            if (warningRecordMap.get(officeKey).get(isSuspend).get(gKey).get(dKey) == null) {
                warningRecordMap.get(officeKey).get(isSuspend).get(gKey).put(dKey, new HashMap<String, String>());
            }
            if (warningRecordMap.get(officeKey).get(isSuspend).get(gKey).get(dKey).get(ruleId) == null) {
                warningRecordMap.get(officeKey).get(isSuspend).get(gKey).get(dKey).put(ruleId, pKey);
            }
        }
        return warningRecordMap;
    }

    // 刷新 existRule
    public Map<String,HashSet<String>> getExistRuleSet() {
        if (EhCacheUtils.get(EhCacheUtils.NOT_RECOVERY_WARNING_RECORD_RULE_SET_IN_MAP_INFO_CACHE) == null) {
            HashMap<String,Set<String>> map = Maps.newHashMap();
            Set<String> existRule = new HashSet<>();
            List<WarningRecord> list = dao.warningRecordList();
            for (WarningRecord warningRecord : list) {
                String officeKey = warningRecord.getOfficeCode();
                String supOfficeKey= officeKey.substring(0,officeKey.length()-3);
                String isSuspend = warningRecord.getIsSuspend();
                String gKey = warningRecord.getSgroupId();
                String dKey = warningRecord.getItemId();
                String pKey = warningRecord.getParamId();
                String ruleId = warningRecord.getRuleId();
                String slgdprId = officeKey + "@" + isSuspend + "@" + gKey + "@" + dKey + "@" + pKey + "@" + ruleId;
                if (!existRule.contains(slgdprId)) {
                    existRule.add(slgdprId);
                    if(map.get(supOfficeKey) == null){
                        map.put(supOfficeKey, Sets.newHashSet());
                    }
                    map.get(supOfficeKey).add(slgdprId);
                }
            }
            EhCacheUtils.put(EhCacheUtils.NOT_RECOVERY_WARNING_RECORD_RULE_SET_IN_MAP_INFO_CACHE, map);
        }
        return (Map<String,HashSet<String>>) EhCacheUtils.get(EhCacheUtils.NOT_RECOVERY_WARNING_RECORD_RULE_SET_IN_MAP_INFO_CACHE);
    }

    // 刷新 warningMap
    public Map<String, Map<String, Object>> getExistRuleMap() {
        Map<String, Map<String, Object>> warningMap = new HashMap<>();//存储所有触发的规则
        List<WarningRecord> list = dao.warningRecordList();
        String officeKey ="";
        for (WarningRecord warningRecord : list) {
            officeKey = warningRecord.getOfficeCode();
            String sGrpName = warningRecord.getSgroupName();
            String sGrpId = warningRecord.getSgroupId();
            String dKey = warningRecord.getItemId();
            String createDate = TimeUtils.getStringFormatDate(warningRecord.getOccurDate());
            String remarks = warningRecord.getRemarks();
            String isSuspend = warningRecord.getIsSuspend();
            if (isSuspend.equals("4")) {
                isSuspend = "2";
            }
            Map<String, String> maps = new HashMap<>();
            maps.put("sg",sGrpName);
            maps.put("dn", basicInfoService.findAvailableDevice().get(dKey));
            maps.put("rm", remarks);
            maps.put("cd", createDate);
            maps.put("wn", isSuspend);
            if (warningMap.get(officeKey) == null) {
                warningMap.put(officeKey, new HashMap<String, Object>());//List<Map<String, String>>
            }
            if (warningMap.get(officeKey).get(isSuspend) == null) {
                warningMap.get(officeKey).put(isSuspend, new ArrayList<Map<String, String>>());
            }
            ((List) warningMap.get(officeKey).get(isSuspend)).add(maps);
        }
//            //临时使用：只显示不为断线的告警信息
//                        if (!isSuspend.equals("4")) {
//            Map<String, String> maps = new HashMap<>();
//            maps.put("sg",sGrpName);
//            maps.put("dn", basicInfoService.findAvailableDevice().get(dKey));
//            maps.put("rm", remarks);
//            maps.put("cd", createDate);
//            maps.put("wn", isSuspend);
//            if (warningMap.get(officeKey) == null) {
//                warningMap.put(officeKey, new HashMap<String, Object>());//List<Map<String, String>>
//            }
//            if (warningMap.get(officeKey).get(isSuspend) == null) {
//                warningMap.get(officeKey).put(isSuspend, new ArrayList<Map<String, String>>());
//            }
//                            ((List) warningMap.get(officeKey).get(isSuspend)).add(maps);            }
//
//        }
        Map<String, String> iscon = findAvailableSgrpAndDeviceId();
        for (String key : iscon.keySet()) {
            if(warningMap.get(key) == null) {
                warningMap.put(key, new HashMap<String, Object>());//List<Map<String, String>>
            }
            warningMap.get(key).put("iscon", iscon.get(key));
        }
//        if (warningMap.size() == 0) {
//            HashMap<String, String> officeHM = basicInfoService.findGradeFourAvailableOffice();
//            for (Map.Entry<String, String> officeEntry : officeHM.entrySet()) {
//                if (warningMap.get(officeEntry.getKey()) == null) {
//                    warningMap.put(officeEntry.getKey(), new HashMap<String, Object>());//List<Map<String, String>>
//                }
//            }
//        }
//        for (Map.Entry<String, Map<String, Object>> office : warningMap.entrySet()) {
//            if ((office.getValue()).get("iscon") == null) {
//                office.getValue().put("iscon", new HashMap<String, String>());
//            }
//            Map<String, String> sd = (Map<String, String>) ((Map<String, Object>) office.getValue()).get("iscon");
//            for (Map.Entry<String, String> device : basicInfoService.findAvailableDevice().entrySet()) {
//                if (sd.get(device.getKey()) == null) {
//                    sd.put(device.getKey(), "on");
//                }
//            }
//        }
        return warningMap;
    }
    public Map<String, String> findAvailableSgrpAndDeviceId() {
        return basicInfoService.findAvailableSgrpAndDeviceId();
    }

    /**
     * 保存一条告警记录
     */
    public void saveNewRule(String sKey, String level, String gKey, String dKey, String pKey, String rKey, String operator, String remarks) {
        WarningRecord warningRecord = new WarningRecord();
        warningRecord.setOfficeCode(sKey);
        warningRecord.setIsSuspend(level);
        warningRecord.setSgroupId(gKey);
        warningRecord.setItemId(dKey);
        warningRecord.setParamId(pKey);
        warningRecord.setRuleId(rKey);
        warningRecord.setOperator(operator);
        warningRecord.setRemarks(remarks);
        super.save(warningRecord);
    }
}
